package com.cimpm.networkDisk.controller;

import com.cimpm.networkDisk.entity.paramEntity.DownloadParam;
import com.cimpm.networkDisk.service.impl.NetworkDiskFileServiceImpl;
import com.cimpm.networkDisk.utils.NetworkDiskFileIOUtil;
import com.hby.common.annotation.aspect.BaseProcess;
import com.hby.common.annotation.aspect.NoEntityAutoProcess;
import com.hby.common.annotation.aspect.NoEntityAutoSingleProcess;
import com.hby.common.annotation.handler.AutoRequestBody;
import com.hby.common.entities.ResMsg;
import com.hby.common.upload.entity.MultipartFileParam;
import com.hby.common.upload.service.ChunkUploadServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/networkFile")
@CrossOrigin
public class NetworkFileController {

    @Autowired
    private NetworkDiskFileIOUtil fileIOUtil;
    @Autowired
    private NetworkDiskFileServiceImpl networkDiskFileService;
    @Autowired
    private ChunkUploadServiceImpl chunkUploadService;

    //获取文件夹下内容; path以/开头
    @RequestMapping("/folderContainer")
    @NoEntityAutoProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "path",NO_VALID_FIELD = "",LOG_MSG = "文件夹内容",
            MENU_CODE = 1105,PERMISSION_TYPE="5")
    public Object getCurrentFolderContainer(@AutoRequestBody Map<String, Object> param){
        return null;
    }

    @RequestMapping("/searchFile")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "",NO_VALID_FIELD = "keyWord",LOG_MSG = "文件夹内容",
            MENU_CODE = 1105,PERMISSION_TYPE="5",USE_USERINFO = true)
    public Object searchFile(@AutoRequestBody Map<String, Object> param){
        return null;
    }

    @RequestMapping("/editFileName")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "name,newName,path",NO_VALID_FIELD = "",LOG_MSG = "更改文件名",
                MENU_CODE = 1105,PERMISSION_TYPE="3",USE_USERINFO = true)
    public Object editFileName(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    @RequestMapping("/editFolderName")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "name,newName,path",NO_VALID_FIELD = "",LOG_MSG = "更改文件夹名",
                MENU_CODE = 1105,PERMISSION_TYPE="3",USE_USERINFO = true)
    public Object editFolderName(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    //如果传过相同文件，则转存；没有返回文件最终命名
    @RequestMapping("/computeUploadMd5")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,
            PARAM_FIELD = "name,path,md5",NO_VALID_FIELD = "",LOG_MSG = "秒传校验",
            MENU_CODE = 1105,PERMISSION_TYPE="2",USE_USERINFO = true)
    public Object computeUploadMd5(@AutoRequestBody Map<String, Object> param){
        return null;
    }

    @GetMapping("/uploadFile")
    public ResMsg uploadGet() {
        return ResMsg.succeseeResOneMes("ok",1001);
    }

    @RequestMapping("/uploadFile")
    @BaseProcess(LOG_MSG = "上传文件",MENU_CODE = 1105,PERMISSION_TYPE = "2",VALID_PARAM = false)
    public ResMsg upload(MultipartFileParam param, HttpServletRequest httpServletRequest) throws Exception {
        Integer userId = Integer.parseInt(httpServletRequest.getAttribute("userId").toString());
        String filePath = httpServletRequest.getHeader("filePath");
        String path = userId + filePath;
        String finalName = NetworkDiskFileIOUtil.hasSameName(NetworkDiskFileIOUtil.getStorePath() + path, param.getFilename());
        if (!finalName.equals(param.getFilename())) throw new Exception("有重名文件");
        chunkUploadService.chunkUploadByMappedByteBuffer(param,path);
        return ResMsg.succeseeResOneMes("上传成功",1001);
    }

    @RequestMapping("/createFolder")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "path,name",NO_VALID_FIELD = "",LOG_MSG = "创建文件夹",
                MENU_CODE = 1105,PERMISSION_TYPE="2",USE_USERINFO = true)
    public Object createFolder(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    @RequestMapping("/download")
    @BaseProcess(LOG_MSG = "下载文件",MENU_CODE = 1105,PERMISSION_TYPE = "5",VALID_PARAM = false)
    public void download(DownloadParam param, HttpServletResponse response) throws Throwable{
        String type = new MimetypesFileTypeMap().getContentType(param.getName());
        String info = new String(param.getName().getBytes("utf-8"), "iso-8859-1");

        response.reset();
        response.setHeader("Content-type",type);
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + info);
        response.addHeader("Access-Control-Allow-Origin", "*");
        String path = param.getUserId()+param.getPath()+param.getName();
        networkDiskFileService.download(param, response);
    }

    @RequestMapping("/preview")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "path,name",NO_VALID_FIELD = "",LOG_MSG = "文件预览",
            MENU_CODE = 1105,PERMISSION_TYPE="5",USE_USERINFO = true)
    public Object preview(@AutoRequestBody Map<String, Object> param){
        return null;
    }

    @RequestMapping("/deleteFile")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "path,name",NO_VALID_FIELD = "",LOG_MSG = "删除文件",
                MENU_CODE = 1105,PERMISSION_TYPE="4",USE_USERINFO = true)
    public Object deleteFile(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    @RequestMapping("/recoverFile")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "recycleBinId",NO_VALID_FIELD = "",LOG_MSG = "恢复文件",
                MENU_CODE = 1105,PERMISSION_TYPE="3",USE_USERINFO = true)
    public Object recoverFile(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    @RequestMapping("/deleteFolder")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "path,name",NO_VALID_FIELD = "",LOG_MSG = "删除文件夹",
                MENU_CODE = 1105,PERMISSION_TYPE="4",USE_USERINFO = true)
    public Object deleteFolder(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    @RequestMapping("/recoverFolder")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "recycleBinId",NO_VALID_FIELD = "",LOG_MSG = "恢复文件",
            MENU_CODE = 1105,PERMISSION_TYPE="3",USE_USERINFO = true)
    public Object recoverFolder(@AutoRequestBody Map<String, Object> param){
        return null;
    }

    @RequestMapping("/mvFile")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "",NO_VALID_FIELD = "",LOG_MSG = "",
                MENU_CODE = 1105,PERMISSION_TYPE="3",USE_USERINFO = true)
    public Object mvFile(@AutoRequestBody Map<String, Object> param){
         return null;
    }

    @RequestMapping("/mvFolder")
    @NoEntityAutoSingleProcess(SERVICE_CLASS = NetworkDiskFileServiceImpl.class,PARAM_FIELD = "",NO_VALID_FIELD = "",LOG_MSG = "",
                MENU_CODE = 1105,PERMISSION_TYPE="2",USE_USERINFO = true)
    public Object mvFolder(@AutoRequestBody Map<String, Object> param){
         return null;
    }
}
